package search;

public class BinarySearch {
    //二分查找
    public static void main(String[] args) {
        int[] arr={2,5,8,9,12,16,18,28,36};
        int key=1;
        int i=binarysearch(arr,key);
        System.out.println(i);


    }
    public static int binarysearch(int[] arr,int key){
        //1.定义min,mid,end下标
        int min=0;
        int max=arr.length-1;
        int mid=(min+max)/2;

        //3.循环
        while(min<=max){

            if(arr[mid]>key){
                max=mid-1;
                mid=(min+max)/2;
            }
            else if(arr[mid]<key){
                min=mid+1;
                mid=(min+max)/2;

            }
            else {
                return mid;
            }
        }
        return -1;
    }
}
